package lucene;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;


public class SegementDemo {
	private static final File INDEX_DIR = new File("../mydemos/resource/index/");

	public static void main(String[] args) throws CorruptIndexException, IOException {
		
		IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR),new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
				writer.setUseCompoundFile(false);
				String docDir = "../mydemos/resource/lucene/";
				indexDocs(writer, docDir);//docDir
				writer.commit();//提交两篇文档，形成_0 段。
//				writer.deleteDocuments(new Term("contents", "school"));//删除文档二
//				writer.commit();//提交删除，形成_0_1.del
//				indexDocs(writer, docDir);//再次索引两篇文档，Lucene 不能判别文档与文档的不同，因而算两
//				篇新的文档。
//				writer.commit();//提交两篇文档，形成_1 段
//				writer.deleteDocuments(new Term("contents", "school"));//删除第二次添加的文档二
				writer.close();//提交删除，形成_1_1.del
	}

	private static void indexDocs(IndexWriter writer, String docDir) throws CorruptIndexException, IOException {
		Document d = new Document();
		File file = new File(docDir);
		for(String f : file.list()) {
			BufferedReader r = new BufferedReader(new FileReader(docDir + f));
			String s;
			StringBuffer sb = new StringBuffer();
			while((s = r.readLine()) != null) {
				sb.append(s);
			}
	        d.add(new Field("contents", sb.toString(), Field.Store.YES, Field.Index.ANALYZED));
	        writer.addDocument(d);

		}
		
	}
	static class SinglePayloadTokenStream extends TokenStream {

		@Override
		public boolean incrementToken() throws IOException {
			// TODO Auto-generated method stub
			return false;
		}
	
	}
	
	
}
